package com.hdax.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CusDrive;
import com.hdax.entity.CusIntention;
import com.hdax.entity.CusOpportunity;
import com.hdax.service.CusDriveService;
import com.hdax.service.CusOpportunityService;
import com.hdax.vo.ActiveUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试驾表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/cusDrive")
public class CusDriveController {

    @Autowired
    private CusDriveService cusDriveService;
    @Autowired
    private CusOpportunityService cusOpportunityService;

    @GetMapping("/toDriveAdd")
    public ModelAndView toDriveAdd() {
        return new ModelAndView("cus_drive/drive_create");
    }

    @PostMapping("/findCusDriveNotHandle")
    public IPage<CusDrive> findCusDriveNotHandle(Integer page, Integer limit) {
        IPage<CusIntention> iPage = new Page<>(page, limit);
        IPage<CusDrive> cusDriveNotHandle = cusDriveService.findCusDriveNotHandle(iPage, new HashMap());
        return cusDriveNotHandle;
    }

    @PostMapping("/findCusDriveNotHandleConditional")
    public IPage<CusDrive> findCusDriveNotHandleConditional(@RequestBody Map map) {
        IPage<CusDrive> iPage = new Page<>(Long.parseLong(map.get("page").toString()),Long.parseLong(map.get("limit").toString()));
        return cusDriveService.findCusDriveNotHandle(iPage, map);
    }


    @GetMapping("/toDriveAddPage")
    public ModelAndView toDriveAddPage(Integer id) {
        ModelAndView modelAndView = new ModelAndView("cus_drive/drive_create_page");
        if (id != null) {
            CusDrive cusDrive = cusDriveService.getById(id);
            modelAndView.addObject("cusDrive", cusDrive);
        }
        return modelAndView;
    }

    @PostMapping("/saveCusDrive")
    public String saveCusDrive(CusDrive cusDrive) {
        cusDrive.setCreateTime(LocalDateTime.now());
        cusDriveService.save(cusDrive);
        return "success";
    }
    @GetMapping("/toDriveProcess")
    public ModelAndView toDriveProcess(){
        return new ModelAndView("cus_drive/drive_process");
    }

    @PostMapping("/findCusDriveNotHandleAndUser")
    public IPage<CusDrive> findCusDriveNotHandleAndUser(Integer page,Integer limit){
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser)subject.getPrincipal();
        IPage<CusIntention> iPage = new Page<>(page, limit);
        Map map = new HashMap<>();
        map.put("userId",activeUser.getId());
        return cusDriveService.findCusDriveNotHandle(iPage,map);
    }
    @PostMapping("/findCusDriveNotHandleAndUserConditional")
    public IPage<CusDrive> findCusDriveNotHandleAndUserConditional(@RequestBody Map map){
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser)subject.getPrincipal();
        IPage<CusDrive> iPage = new Page<>(Long.parseLong(map.get("page").toString()),Long.parseLong(map.get("limit").toString()));
        map.put("userId",activeUser.getId());
        return cusDriveService.findCusDriveNotHandle(iPage,map);
    }


    @PostMapping("/cancel")
    public String cancel(Integer id){
        String updateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        cusDriveService.updateCusDriveCancel(id,updateTime);
        return "success";
    }

    @GetMapping("/toCusDriveArchive")
    public ModelAndView toCusDriveArchive(){
        return new ModelAndView("cus_drive/drive_archive");
    }

    @PostMapping("/findCusDriveIsHandle")
    public IPage<CusDrive> findCusDriveIsHandle(Integer page, Integer limit) {
        IPage<CusIntention> iPage = new Page<>(page, limit);
        IPage<CusDrive> cusDriveNotHandle = cusDriveService.findCusDriveIsHandle(iPage, new HashMap());
        return cusDriveNotHandle;
    }

    @PostMapping("/findCusDriveIsHandleConditional")
    public IPage<CusDrive> findCusDriveIsHandleConditional(@RequestBody Map map) {
        IPage<CusDrive> iPage = new Page<>(Long.parseLong(map.get("page").toString()),Long.parseLong(map.get("limit").toString()));
        IPage<CusDrive> cusDriveNotHandle = cusDriveService.findCusDriveIsHandle(iPage, map);
        return cusDriveNotHandle;
    }

    @GetMapping("/toDriveProcessPage")
    public ModelAndView toDriveProcessPage(Integer id){
        ModelAndView modelAndView = new ModelAndView("cus_drive/drive_process_page");
        CusDrive cusDrive = cusDriveService.getById(id);
        modelAndView.addObject("cusDrive", cusDrive);
        return modelAndView;
    }

    @PostMapping("/fileUpLoad")
    public Map fileUpLoad(@RequestParam("file") MultipartFile multipartFile, String msg,Integer id) throws IOException {
        File d = new File(this.getClass().getResource("/").getPath());
        System.out.println(id);
        String folder = d.getParentFile().getParentFile().getParentFile().getParentFile().toString()+
                "\\src\\main\\webapp\\img\\";
        multipartFile.transferTo(new File(folder+multipartFile.getOriginalFilename()));
        System.out.println(folder);
        String path = "../../../img/"+multipartFile.getOriginalFilename();
        if (msg.equals("身份证")){
            cusDriveService.updateIDCardSource(path,id);
        }else {
            cusDriveService.updateDriveCardSource(path,id);
        }
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","success");
        return map;
    }

    @PostMapping("/updateNotes")
    public String updateNotes(String notes,Integer id){
        String updateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        cusDriveService.updateNotes(notes,id,updateTime);
        CusDrive cusDrive = cusDriveService.getById(id);
        CusOpportunity cusOpportunity = new CusOpportunity();
        cusOpportunity.setCusId(cusDrive.getCusId());
        cusOpportunity.setCreateTime(LocalDateTime.now());
        cusOpportunity.setOpportunityState(1);
        cusOpportunity.setProbably("30%");
        cusOpportunityService.save(cusOpportunity);
        return "success";
    }

    @GetMapping("/toDriveArchivePage")
    public ModelAndView toDriveArchivePage(Integer id){
        ModelAndView modelAndView = new ModelAndView("cus_drive/drive_archive_page");
        CusDrive cusDrive = cusDriveService.getById(id);
        modelAndView.addObject("cusDrive", cusDrive);
        return modelAndView;
    }

    @PostMapping("/findCusDriveCount")
    public Map findCusDriveCount(){
        Map map = new HashMap();
        map.put("month",cusDriveService.findThisMonthCusDriveCount());
        map.put("all",cusDriveService.findCusDriveCount());
        return map;
    }

    @PostMapping("/findCusDriveChainCompare")
    public Float findCusDriveChainCompare(){
        return cusDriveService.findCusDriveChainCompare();
    }

    @PostMapping("/findThisYearCusDriveIncre")
    public List findThisYearCusDriveIncre(){
        Map thisYearCusDriveIncre = cusDriveService.findThisYearCusDriveIncre();
        ArrayList arrayList = new ArrayList<>(thisYearCusDriveIncre.values());
        return arrayList;
    }

}

